<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class OxUser 
{
	public $iduser;
    public $name;
    public $address;
    public $email;
    public $username;
    public $info;
    public $date_joined;
    
    private $articles;
    private $articles_read;
    
    private $actions;
    private $actions_read;
    
    /* @var $roles OxUser */
    private $roles;
    private $roles_read;
    
    /*
     		$query = $this->db->get_where('articles', 'iduser = ' . $user->iduser);
		return $query->result();

     */
    
    public static function from_id($iduser)
    {
        $new_user = new OxUser();
        $new_user->load($iduser);
        return $new_user;
    }
    public static function from_username($username)
    {
        $new_user = new OxUser();
        $new_user->load_by_username($username);
        return $new_user;
    }
    
    function __construct()
    {
		$this->clear();
	}
    
    function clear()
    {
        $this->iduser = null;
        $this->name = null;
        $this->address = null;
        $this->email = null;
        $this->username = null;
        $this->info = null;
        $this->date_joined = null;
        $this->articles_read = false;
        $this->articles = array();
        $this->actions_read = false;
        $this->actions = array();
        $this->roles_read = false;
        $this->roles = array();
        return $this;
    }
    
    private function object_from_db_data($userdata)
    {
        $this->iduser = $userdata->iduser;
        $this->name = $userdata->name;
        $this->address = $userdata->address;
        $this->email = $userdata->email;
        $this->username = $userdata->username;
        $this->info = $userdata->info;
        $this->date_joined = $userdata->date_joined;
    }
    
    function load_by_username($username_to_load)
    {
        $CI =& get_instance();
        
		$query = $CI->db->select('*')
                              ->from('users')
                              ->where('username',$username_to_load)
                              ->get()->result();
		if(!$query)
        {
            $this->clear();
        }
        else
        {
            $userdata = $query[0];
            $this->object_from_db_data($userdata);
        }
        return $this;
    }	    

    
    function load($id)
    {
        $CI =& get_instance();
        
		$query = $CI->db->select('*')
                              ->from('users')
                              ->where('iduser',$id)
                              ->get()->result();
		if(!$query)
        {
            $this->clear();
        }
        else
        {
            $userdata = $query[0];
            $this->object_from_db_data($userdata);
        }
        return $this;
    }

    public function articles()
    {
        $CI =& get_instance();
        if(!$this->articles_read)
        {
            //read articles, create article objects
            $query = $CI->db->select('idarticle')
                              ->from('articles')
                              ->where('iduser = ', $this->iduser)
                              ->get()->result();
            foreach($query as $art_stub)
            {
                $this->articles[] = OxArticle::from_id($art_stub->idarticle);
            }
            $this->articles_read = true;
        }
        return $this->articles;
    }
    
    public function actions()
    {
        if(!$this->actions_read)
        {
            $CI =& get_instance();
            $this->actions = $CI->flags->get_users_flags($this);

            $this->actions_read = true;
        }
        return $this->actions;
    }
    
    public function roles()
    {
        if(!$this->roles_read)
        {
            $CI =& get_instance();
            $query = $CI->db->select('idorganization, role')
                              ->from('organizations_users')
                              ->where('iduser = ', $this->iduser)
                              ->get()->result();
            foreach($query as $role)
            {
                $role->organization = OxOrganization::from_id($role->idorganization);
                $this->roles[] = $role;
            }
            $this->roles_read = true;
        }
        return $this->roles;
    }
    
    public function name_link($class = "", $style = "")
    {
        $s = '<a href="' . site_url('profile/' . $this->iduser) . '"';
        if($class) { $s .= ' class="'.$class.'"'; }
        if($style) { $s .= ' style="'.$style.'"'; }
        $s .= '>' . $this->name . '</a>';
        return $s;
    }
    
} 